<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9272272.html">Redis（四）Redis高级</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2>一Redis 数据备份与恢复</h2>
<div class="article-body">
<div id="content" class="article-intro">
<p>Redis&nbsp;<strong>SAVE</strong>&nbsp;命令用于创建当前数据库的备份。</p>
<p><strong>语法</strong></p>
<p>redis Save 命令基本语法如下：</p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SAVE </span></span></span></span></span></span></span></span></pre>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SAVE 
OK</span></span></span></span></span></span></span></span></pre>
<p>该命令将在 redis 安装目录中创建dump.rdb文件。</p>
<p><span style="font-size: 18px;"><strong>恢复数据</strong></span></p>
如果需要恢复数据，只需将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可。获取 redis 目录可以使用&nbsp;<strong>CONFIG</strong>&nbsp;命令，如下所示：
<pre class="prettyprint prettyprinted"><span class="pln"> 
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> CONFIG GET dir
<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"dir"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">"/usr/local/redis/bin"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>以上命令&nbsp;<strong>CONFIG GET dir</strong>&nbsp;输出的 redis 安装目录为 /usr/local/redis/bin。</p>
<p><span style="font-size: 18px;"><strong>Bgsave</strong></span></p>
<p>创建 redis 备份文件也可以使用命令&nbsp;<strong>BGSAVE</strong>，该命令在后台执行。</p>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> BGSAVE

<span class="typ">Background<span class="pln"> saving started</span></span></span></span></span></span></span></span></span></pre>
</div>
</div>
<div class="previous-next-links">
<h2>二Redis 安全</h2>
<p>我们可以通过 redis 的配置文件设置密码参数，这样客户端连接到 redis 服务就需要密码验证，这样可以让你的 redis 服务更安全。</p>
<p><strong>实例</strong></p>
<p>我们可以通过以下命令查看是否设置了密码验证：</p>
<pre class="prettyprint prettyprinted"><span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> CONFIG <span class="kwd">get<span class="pln"> requirepass
<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"requirepass"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">""</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>默认情况下 requirepass 参数是空的，这就意味着你无需通过密码验证就可以连接到 redis 服务。</p>
<p>你可以通过以下命令来修改该参数：</p>
<pre class="prettyprint prettyprinted"><span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> CONFIG <span class="kwd">set<span class="pln"> requirepass <span class="str">"runoob"<span class="pln">
OK
<span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> CONFIG <span class="kwd">get<span class="pln"> requirepass
<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"requirepass"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">"runoob"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>设置密码后，客户端连接 redis 服务就需要密码验证，否则无法执行命令。</p>
<p><strong>语法</strong></p>
<p><strong>AUTH</strong>&nbsp;命令基本语法格式如下：</p>
<pre class="prettyprint prettyprinted"><span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> AUTH password</span></span></span></span></span></span></span></pre>
<p>实例<span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> AUTH <span class="str">"runoob"</span></span></span></span></span></span></span></span></p>
<pre class="prettyprint prettyprinted"><span class="lit"><span class="pun"><span class="lit"><span class="pun"><span class="lit"><span class="pun"><span class="pln"><span class="str"><span class="pln">OK
<span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SET mykey <span class="str">"Test value"<span class="pln">
OK
<span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> GET mykey
<span class="str">"Test value"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<h2 style="text-align: left;">三Redis 性能测试</h2>
<p>Redis 性能测试是通过同时执行多个命令实现的。</p>
<p><strong>语法</strong></p>
<p>redis 性能测试的基本命令如下：</p>
<pre class="prettyprint prettyprinted"><span class="pln">redis<span class="pun">-<span class="pln">benchmark <span class="pun">[<span class="pln">option<span class="pun">]<span class="pln"> <span class="pun">[<span class="pln">option value<span class="pun">]</span></span></span></span></span></span></span></span></span></span></pre>
<p><strong>实例</strong></p>
<p>以下实例同时执行 10000 个请求来检测性能：</p>
<pre class="prettyprint prettyprinted"><span class="pln">$ redis<span class="pun">-<span class="pln">benchmark <span class="pun">-<span class="pln">n <span class="lit">10000<span class="pln">  <span class="pun">-<span class="pln">q

PING_INLINE<span class="pun">:<span class="pln"> <span class="lit">141043.72<span class="pln"> requests per second
PING_BULK<span class="pun">:<span class="pln"> <span class="lit">142857.14<span class="pln"> requests per second
SET<span class="pun">:<span class="pln"> <span class="lit">141442.72<span class="pln"> requests per second
GET<span class="pun">:<span class="pln"> <span class="lit">145348.83<span class="pln"> requests per second
INCR<span class="pun">:<span class="pln"> <span class="lit">137362.64<span class="pln"> requests per second
LPUSH<span class="pun">:<span class="pln"> <span class="lit">145348.83<span class="pln"> requests per second
LPOP<span class="pun">:<span class="pln"> <span class="lit">146198.83<span class="pln"> requests per second
SADD<span class="pun">:<span class="pln"> <span class="lit">146198.83<span class="pln"> requests per second
SPOP<span class="pun">:<span class="pln"> <span class="lit">149253.73<span class="pln"> requests per second
LPUSH <span class="pun">(<span class="pln">needed to benchmark LRANGE<span class="pun">):<span class="pln"> <span class="lit">148588.42<span class="pln"> requests per second
LRANGE_100 <span class="pun">(<span class="pln">first <span class="lit">100<span class="pln"> elements<span class="pun">):<span class="pln"> <span class="lit">58411.21<span class="pln"> requests per second
LRANGE_300 <span class="pun">(<span class="pln">first <span class="lit">300<span class="pln"> elements<span class="pun">):<span class="pln"> <span class="lit">21195.42<span class="pln"> requests per second
LRANGE_500 <span class="pun">(<span class="pln">first <span class="lit">450<span class="pln"> elements<span class="pun">):<span class="pln"> <span class="lit">14539.11<span class="pln"> requests per second
LRANGE_600 <span class="pun">(<span class="pln">first <span class="lit">600<span class="pln"> elements<span class="pun">):<span class="pln"> <span class="lit">10504.20<span class="pln"> requests per second
MSET <span class="pun">(<span class="lit">10<span class="pln"> keys<span class="pun">):<span class="pln"> <span class="lit">93283.58<span class="pln"> requests per second</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>redis 性能测试工具可选参数如下所示：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>选项</th><th>描述</th><th>默认值</th></tr>
<tr>
<td>1</td>
<td><strong>-h</strong></td>
<td>指定服务器主机名</td>
<td>127.0.0.1</td>
</tr>
<tr>
<td>2</td>
<td><strong>-p</strong></td>
<td>指定服务器端口</td>
<td>6379</td>
</tr>
<tr>
<td>3</td>
<td><strong>-s</strong></td>
<td>指定服务器 socket</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>4</td>
<td><strong>-c</strong></td>
<td>指定并发连接数</td>
<td>50</td>
</tr>
<tr>
<td>5</td>
<td><strong>-n</strong></td>
<td>指定请求数</td>
<td>10000</td>
</tr>
<tr>
<td>6</td>
<td><strong>-d</strong></td>
<td>以字节的形式指定 SET/GET 值的数据大小</td>
<td>2</td>
</tr>
<tr>
<td>7</td>
<td><strong>-k</strong></td>
<td>1=keep alive 0=reconnect</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td><strong>-r</strong></td>
<td>SET/GET/INCR 使用随机 key, SADD 使用随机值</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>9</td>
<td><strong>-P</strong></td>
<td>通过管道传输 &lt;numreq&gt; 请求</td>
<td>1</td>
</tr>
<tr>
<td>10</td>
<td><strong>-q</strong></td>
<td>强制退出 redis。仅显示 query/sec 值</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>11</td>
<td><strong>--csv</strong></td>
<td>以 CSV 格式输出</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>12</td>
<td><strong>-l</strong></td>
<td>生成循环，永久执行测试</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>13</td>
<td><strong>-t</strong></td>
<td>仅运行以逗号分隔的测试命令列表。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>14</td>
<td><strong>-I</strong></td>
<td>Idle 模式。仅打开 N 个 idle 连接并等待。</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p><strong>实例</strong></p>
<p>以下实例我们使用了多个参数来测试 redis 性能：</p>
<pre class="prettyprint prettyprinted"><span class="pln">$ redis<span class="pun">-<span class="pln">benchmark <span class="pun">-<span class="pln">h <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pln"> <span class="pun">-<span class="pln">p <span class="lit">6379<span class="pln"> <span class="pun">-<span class="pln">t <span class="kwd">set<span class="pun">,<span class="pln">lpush <span class="pun">-<span class="pln">n <span class="lit">10000<span class="pln"> <span class="pun">-<span class="pln">q

SET<span class="pun">:<span class="pln"> <span class="lit">146198.83<span class="pln"> requests per second
LPUSH<span class="pun">:<span class="pln"> <span class="lit">145560.41<span class="pln"> requests per second</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>以上实例中主机为 127.0.0.1，端口号为 6379，执行的命令为 set,lpush，请求数为 10000，通过 -q 参数让结果只显示每秒执行的请求数。</p>
<h2>四Redis 客户端连接</h2>
<p>Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接，当一个连接建立后，Redis 内部会进行以下一些操作：</p>
<ul>
<li>首先，客户端 socket 会被设置为非阻塞模式，因为 Redis 在网络事件处理上采用的是非阻塞多路复用模型。</li>
<li>然后为这个 socket 设置 TCP_NODELAY 属性，禁用 Nagle 算法</li>
<li>然后创建一个可读的文件事件用于监听这个客户端 socket 的数据发送</li>
</ul>
<p><span style="font-size: 18px;"><strong>最大连接数</strong></span></p>
<p>在 Redis2.4 中，最大连接数是被直接硬编码在代码里面的，而在2.6版本中这个值变成可配置的。</p>
<p>maxclients 的默认值是 10000，你也可以在 redis.conf 中对这个值进行修改。</p>
<pre class="prettyprint">config get maxclients

1) "maxclients"
2) "10000"
</pre>
<p><strong>实例</strong></p>
<p>以下实例我们在服务启动时设置最大连接数为 100000：</p>
<pre class="prettyprint">redis-server --maxclients 100000
</pre>
<p><strong><span style="font-size: 18px;">客户端命令</span></strong></p>
<table class="reference">
<tbody>
<tr><th>S.N.</th><th>命令</th><th>描述</th></tr>
<tr>
<td>1</td>
<td><strong>CLIENT LIST</strong></td>
<td>返回连接到 redis 服务的客户端列表</td>
</tr>
<tr>
<td>2</td>
<td><strong>CLIENT SETNAME</strong></td>
<td>设置当前连接的名称</td>
</tr>
<tr>
<td>3</td>
<td><strong>CLIENT GETNAME</strong></td>
<td>获取通过 CLIENT SETNAME 命令设置的服务名称</td>
</tr>
<tr>
<td>4</td>
<td><strong>CLIENT PAUSE</strong></td>
<td>挂起客户端连接，指定挂起的时间以毫秒计</td>
</tr>
<tr>
<td>5</td>
<td><strong>CLIENT KILL</strong></td>
<td>关闭客户端连接</td>
</tr>
</tbody>
</table>
<h2>五Redis 管道技术</h2>
<p>Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务。这意味着通常情况下一个请求会遵循以下步骤：</p>
<ul>
<li>客户端向服务端发送一个查询请求，并监听Socket返回，通常是以阻塞模式，等待服务端响应。</li>
<li>服务端处理命令，并将结果返回给客户端。</li>
</ul>
<p><span style="font-size: 18px;"><strong>Redis 管道技术</strong></span></p>
<p>Redis 管道技术可以在服务端未响应时，客户端可以继续向服务端发送请求，并最终一次性读取所有服务端的响应。</p>
<p><strong>实例</strong></p>
<p>查看 redis 管道，只需要启动 redis 实例并输入以下命令：</p>
<pre class="prettyprint prettyprinted"><span class="pln">$<span class="pun">(<span class="pln">echo <span class="pun">-<span class="pln">en <span class="str">"PING\r\n SET runoobkey redis\r\nGET runoobkey\r\nINCR visitor\r\nINCR visitor\r\nINCR visitor\r\n"<span class="pun">;<span class="pln"> sleep <span class="lit">10<span class="pun">)<span class="pln"> <span class="pun">|<span class="pln"> nc localhost <span class="lit">6379<span class="pln">

<span class="pun">+<span class="pln">PONG
<span class="pun">+<span class="pln">OK
redis
<span class="pun">:<span class="lit">1<span class="pln">
<span class="pun">:<span class="lit">2<span class="pln">
<span class="pun">:<span class="lit">3</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>以上实例中我们通过使用&nbsp;<strong>PING</strong>&nbsp;命令查看redis服务是否可用， 之后我们设置了 runoobkey 的值为 redis，然后我们获取 runoobkey 的值并使得 visitor 自增 3 次。</p>
<p>在返回的结果中我们可以看到这些命令一次性向 redis 服务提交，并最终一次性读取所有服务端的响应</p>
<p><span style="font-size: 18px;"><strong>管道技术的优势</strong></span></p>
<p>管道技术最显著的优势是提高了 redis 服务的性能。</p>
<p><span style="font-size: 18px;"><strong>一些测试数据</strong></span></p>
<p>在下面的测试中，我们将使用Redis的Ruby客户端，支持管道技术特性，测试管道技术对速度的提升效果。</p>
<pre class="prettyprint prettyprinted"><span class="kwd">require<span class="pln"> <span class="str">'rubygems'<span class="pln"> 
<span class="kwd">require<span class="pln"> <span class="str">'redis'<span class="pln">
<span class="kwd">def<span class="pln"> bench<span class="pun">(<span class="pln">descr<span class="pun">)<span class="pln"> 
start <span class="pun">=<span class="pln"> <span class="typ">Time<span class="pun">.<span class="pln">now 
<span class="kwd">yield<span class="pln"> 
puts <span class="str">"#{descr} #{Time.now-start} seconds"<span class="pln"> 
<span class="kwd">end<span class="pln">
<span class="kwd">def<span class="pln"> without_pipelining 
r <span class="pun">=<span class="pln"> <span class="typ">Redis<span class="pun">.<span class="kwd">new<span class="pln"> 
<span class="lit">10000.times<span class="pln"> <span class="pun">{<span class="pln"> 
&nbsp;&nbsp;&nbsp;&nbsp;r<span class="pun">.<span class="pln">ping 
<span class="pun">}<span class="pln"> 
<span class="kwd">end<span class="pln">
<span class="kwd">def<span class="pln"> with_pipelining 
r <span class="pun">=<span class="pln"> <span class="typ">Redis<span class="pun">.<span class="kwd">new<span class="pln"> 
r<span class="pun">.<span class="pln">pipelined <span class="pun">{<span class="pln"> 
&nbsp;&nbsp;&nbsp;&nbsp;<span class="lit">10000.times<span class="pln"> <span class="pun">{<span class="pln"> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r<span class="pun">.<span class="pln">ping 
&nbsp;&nbsp;&nbsp;&nbsp;<span class="pun">}<span class="pln"> 
<span class="pun">}<span class="pln"> 
<span class="kwd">end<span class="pln">
bench<span class="pun">(<span class="str">"without pipelining"<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln"> 
&nbsp;&nbsp;&nbsp;&nbsp;without_pipelining 
<span class="pun">}<span class="pln"> 
bench<span class="pun">(<span class="str">"with pipelining"<span class="pun">)<span class="pln"> <span class="pun">{<span class="pln"> 
&nbsp;&nbsp;&nbsp;&nbsp;with_pipelining 
<span class="pun">}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>从处于局域网中的Mac OS X系统上执行上面这个简单脚本的数据表明，开启了管道操作后，往返时延已经被改善得相当低了。</p>
<pre class="prettyprint prettyprinted"><span class="pln">without pipelining <span class="lit">1.185238<span class="pln"> seconds 
<span class="kwd">with<span class="pln"> pipelining <span class="lit">0.250783<span class="pln"> seconds</span></span></span></span></span></span></span></pre>
<p>如你所见，开启管道后，我们的速度效率提升了5倍。</p>
<h2>六Redis 分区</h2>
<p>分区是分割数据到多个Redis实例的处理过程，因此每个实例只保存key的一个子集。</p>
<p><span style="font-size: 18px;"><strong>分区的优势</strong></span></p>
<ul>
<li>通过利用多台计算机内存的和值，允许我们构造更大的数据库。</li>
<li>通过多核和多台计算机，允许我们扩展计算能力；通过多台计算机和网络适配器，允许我们扩展网络带宽。</li>
</ul>
<p><strong><span style="font-size: 18px;">分区的不足</span></strong></p>
<p>redis的一些特性在分区方面表现的不是很好：</p>
<ul>
<li>涉及多个key的操作通常是不被支持的。举例来说，当两个set映射到不同的redis实例上时，你就不能对这两个set执行交集操作。</li>
<li>涉及多个key的redis事务不能使用。</li>
<li>当使用分区时，数据处理较为复杂，比如你需要处理多个rdb/aof文件，并且从多个实例和主机备份持久化文件。</li>
<li>增加或删除容量也比较复杂。redis集群大多数支持在运行时增加、删除节点的透明数据平衡的能力，但是类似于客户端分区、代理等其他系统则不支持这项特性。然而，一种叫做presharding的技术对此是有帮助的。</li>
</ul>
<p><span style="font-size: 18px;"><strong>分区类型</strong></span></p>
<p>Redis 有两种类型分区。 假设有4个Redis实例 R0，R1，R2，R3，和类似user:1，user:2这样的表示用户的多个key，对既定的key有多种不同方式来选择这个key存放在哪个实例中。也就是说，有不同的系统来映射某个key到某个Redis服务。</p>
<p><strong><span style="font-size: 18px;">范围分区</span></strong></p>
<p>最简单的分区方式是按范围分区，就是映射一定范围的对象到特定的Redis实例。</p>
<p>比如，ID从0到10000的用户会保存到实例R0，ID从10001到 20000的用户会保存到R1，以此类推。</p>
<p>这种方式是可行的，并且在实际中使用，不足就是要有一个区间范围到实例的映射表。这个表要被管理，同时还需要各 种对象的映射表，通常对Redis来说并非是好的方法。</p>
<p><span style="font-size: 18px;"><strong>哈希分区</strong></span></p>
<p>另外一种分区方法是hash分区。这对任何key都适用，也无需是object_name:这种形式，像下面描述的一样简单：</p>
<ul>
<li>用一个hash函数将key转换为一个数字，比如使用crc32 hash函数。对key foobar执行crc32(foobar)会输出类似93024922的整数。</li>
<li>对这个整数取模，将其转化为0-3之间的数字，就可以将这个整数映射到4个Redis实例中的一个了。93024922 % 4 = 2，就是说key foobar应该被存到R2实例中。注意：取模操作是取除的余数，通常在多种编程语言中用%操作符实现。</li>
</ul>
<h2 class="prettyprint prettyprinted"><span class="pln">七Redis 服务器</span></h2>
<p>Redis 服务器命令主要是用于管理 redis 服务。</p>
<p><strong>实例</strong></p>
<p>以下实例演示了如何获取 redis 服务器的统计信息：</p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> INFO<br /></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code" onclick="cnblogs_code_show('f6b45645-af75-43fc-8eb9-454c800420d4')"><img id="code_img_closed_f6b45645-af75-43fc-8eb9-454c800420d4" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /><img id="code_img_opened_f6b45645-af75-43fc-8eb9-454c800420d4" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('f6b45645-af75-43fc-8eb9-454c800420d4',event)" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" />
<div id="cnblogs_code_open_f6b45645-af75-43fc-8eb9-454c800420d4" class="cnblogs_code_hide">
<pre><span style="color: #000000;"># Server
redis_version:</span><span style="color: #800080;">2.8</span>.<span style="color: #800080;">13</span><span style="color: #000000;">
redis_git_sha1:</span><span style="color: #800080;">00000000</span><span style="color: #000000;">
redis_git_dirty:</span><span style="color: #800080;">0</span><span style="color: #000000;">
redis_build_id:c2238b38b1edb0e2
redis_mode:standalone
os:Linux </span><span style="color: #800080;">3.5</span>.<span style="color: #800080;">0</span>-<span style="color: #800080;">48</span>-<span style="color: #000000;">generic x86_64
arch_bits:</span><span style="color: #800080;">64</span><span style="color: #000000;">
multiplexing_api:epoll
gcc_version:</span><span style="color: #800080;">4.7</span>.<span style="color: #800080;">2</span><span style="color: #000000;">
process_id:</span><span style="color: #800080;">3856</span><span style="color: #000000;">
run_id:0e61abd297771de3fe812a3c21027732ac9f41fe
tcp_port:</span><span style="color: #800080;">6379</span><span style="color: #000000;">
uptime_in_seconds:</span><span style="color: #800080;">11554</span><span style="color: #000000;">
uptime_in_days:</span><span style="color: #800080;">0</span><span style="color: #000000;">
hz:</span><span style="color: #800080;">10</span><span style="color: #000000;">
lru_clock:</span><span style="color: #800080;">16651447</span><span style="color: #000000;">
config_file:

# Clients
connected_clients:</span><span style="color: #800080;">1</span><span style="color: #000000;">
client</span>-longest_output_list:<span style="color: #800080;">0</span><span style="color: #000000;">
client</span>-biggest_input_buf:<span style="color: #800080;">0</span><span style="color: #000000;">
blocked_clients:</span><span style="color: #800080;">0</span><span style="color: #000000;">

# Memory
used_memory:</span><span style="color: #800080;">589016</span><span style="color: #000000;">
used_memory_human:</span><span style="color: #800080;">575</span><span style="color: #000000;">.21K
used_memory_rss:</span><span style="color: #800080;">2461696</span><span style="color: #000000;">
used_memory_peak:</span><span style="color: #800080;">667312</span><span style="color: #000000;">
used_memory_peak_human:</span><span style="color: #800080;">651</span><span style="color: #000000;">.67K
used_memory_lua:</span><span style="color: #800080;">33792</span><span style="color: #000000;">
mem_fragmentation_ratio:</span><span style="color: #800080;">4.18</span><span style="color: #000000;">
mem_allocator:jemalloc</span>-<span style="color: #800080;">3.6</span>.<span style="color: #800080;">0</span><span style="color: #000000;">

# Persistence
loading:</span><span style="color: #800080;">0</span><span style="color: #000000;">
rdb_changes_since_last_save:</span><span style="color: #800080;">3</span><span style="color: #000000;">
rdb_bgsave_in_progress:</span><span style="color: #800080;">0</span><span style="color: #000000;">
rdb_last_save_time:</span><span style="color: #800080;">1409158561</span><span style="color: #000000;">
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:</span><span style="color: #800080;">0</span><span style="color: #000000;">
rdb_current_bgsave_time_sec:</span>-<span style="color: #800080;">1</span><span style="color: #000000;">
aof_enabled:</span><span style="color: #800080;">0</span><span style="color: #000000;">
aof_rewrite_in_progress:</span><span style="color: #800080;">0</span><span style="color: #000000;">
aof_rewrite_scheduled:</span><span style="color: #800080;">0</span><span style="color: #000000;">
aof_last_rewrite_time_sec:</span>-<span style="color: #800080;">1</span><span style="color: #000000;">
aof_current_rewrite_time_sec:</span>-<span style="color: #800080;">1</span><span style="color: #000000;">
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:</span><span style="color: #800080;">24</span><span style="color: #000000;">
total_commands_processed:</span><span style="color: #800080;">294</span><span style="color: #000000;">
instantaneous_ops_per_sec:</span><span style="color: #800080;">0</span><span style="color: #000000;">
rejected_connections:</span><span style="color: #800080;">0</span><span style="color: #000000;">
sync_full:</span><span style="color: #800080;">0</span><span style="color: #000000;">
sync_partial_ok:</span><span style="color: #800080;">0</span><span style="color: #000000;">
sync_partial_err:</span><span style="color: #800080;">0</span><span style="color: #000000;">
expired_keys:</span><span style="color: #800080;">0</span><span style="color: #000000;">
evicted_keys:</span><span style="color: #800080;">0</span><span style="color: #000000;">
keyspace_hits:</span><span style="color: #800080;">41</span><span style="color: #000000;">
keyspace_misses:</span><span style="color: #800080;">82</span><span style="color: #000000;">
pubsub_channels:</span><span style="color: #800080;">0</span><span style="color: #000000;">
pubsub_patterns:</span><span style="color: #800080;">0</span><span style="color: #000000;">
latest_fork_usec:</span><span style="color: #800080;">264</span><span style="color: #000000;">

# Replication
role:master
connected_slaves:</span><span style="color: #800080;">0</span><span style="color: #000000;">
master_repl_offset:</span><span style="color: #800080;">0</span><span style="color: #000000;">
repl_backlog_active:</span><span style="color: #800080;">0</span><span style="color: #000000;">
repl_backlog_size:</span><span style="color: #800080;">1048576</span><span style="color: #000000;">
repl_backlog_first_byte_offset:</span><span style="color: #800080;">0</span><span style="color: #000000;">
repl_backlog_histlen:</span><span style="color: #800080;">0</span><span style="color: #000000;">

# CPU
used_cpu_sys:</span><span style="color: #800080;">10.49</span><span style="color: #000000;">
used_cpu_user:</span><span style="color: #800080;">4.96</span><span style="color: #000000;">
used_cpu_sys_children:</span><span style="color: #800080;">0.00</span><span style="color: #000000;">
used_cpu_user_children:</span><span style="color: #800080;">0.01</span><span style="color: #000000;">

# Keyspace
db0:keys</span>=<span style="color: #800080;">94</span>,expires=<span style="color: #800080;">1</span>,avg_ttl=<span style="color: #800080;">41638810</span><span style="color: #000000;">
db1:keys</span>=<span style="color: #800080;">1</span>,expires=<span style="color: #800080;">0</span>,avg_ttl=<span style="color: #800080;">0</span><span style="color: #000000;">
db3:keys</span>=<span style="color: #800080;">1</span>,expires=<span style="color: #800080;">0</span>,avg_ttl=<span style="color: #800080;">0</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>下表列出了 redis 服务器的相关命令:</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/server-bgrewriteaof.html">BGREWRITEAOF</a>&nbsp;<br />异步执行一个 AOF（AppendOnly File） 文件重写操作</td>





</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/server-bgsave.html">BGSAVE</a>&nbsp;<br />在后台异步保存当前数据库的数据到磁盘</td>





</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/server-client-kill.html">CLIENT KILL [ip:port] [ID client-id]</a>&nbsp;<br />关闭客户端连接</td>





</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/server-client-list.html">CLIENT LIST</a>&nbsp;<br />获取连接到服务器的客户端连接列表</td>





</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/server-client-getname.html">CLIENT GETNAME</a>&nbsp;<br />获取连接的名称</td>





</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/server-client-pause.html">CLIENT PAUSE timeout</a>&nbsp;<br />在指定时间内终止运行来自客户端的命令</td>





</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/server-client-setname.html">CLIENT SETNAME connection-name</a>&nbsp;<br />设置当前连接的名称</td>





</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/server-cluster-slots.html">CLUSTER SLOTS</a>&nbsp;<br />获取集群节点的映射数组</td>





</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/server-command.html">COMMAND</a>&nbsp;<br />获取 Redis 命令详情数组</td>





</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/server-command-count.html">COMMAND COUNT</a>&nbsp;<br />获取 Redis 命令总数</td>





</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/server-command-getkeys.html">COMMAND GETKEYS</a>&nbsp;<br />获取给定命令的所有键</td>





</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/server-time.html">TIME</a>&nbsp;<br />返回当前服务器时间</td>





</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/server-command-info.html">COMMAND INFO command-name [command-name ...]</a>&nbsp;<br />获取指定 Redis 命令描述的数组</td>





</tr>
<tr>
<td>14</td>
<td><a href="http://www.runoob.com/redis/server-config-get.html">CONFIG GET parameter</a>&nbsp;<br />获取指定配置参数的值</td>





</tr>
<tr>
<td>15</td>
<td><a href="http://www.runoob.com/redis/server-config-rewrite.html">CONFIG REWRITE</a>&nbsp;<br />对启动 Redis 服务器时所指定的 redis.conf 配置文件进行改写</td>





</tr>
<tr>
<td>16</td>
<td><a href="http://www.runoob.com/redis/server-config-set.html">CONFIG SET parameter value</a>&nbsp;<br />修改 redis 配置参数，无需重启</td>





</tr>
<tr>
<td>17</td>
<td><a href="http://www.runoob.com/redis/server-config-resetstat.html">CONFIG RESETSTAT</a>&nbsp;<br />重置 INFO 命令中的某些统计数据</td>





</tr>
<tr>
<td>18</td>
<td><a href="http://www.runoob.com/redis/server-dbsize.html">DBSIZE</a>&nbsp;<br />返回当前数据库的 key 的数量</td>





</tr>
<tr>
<td>19</td>
<td><a href="http://www.runoob.com/redis/server-debug-object.html">DEBUG OBJECT key</a>&nbsp;<br />获取 key 的调试信息</td>





</tr>
<tr>
<td>20</td>
<td><a href="http://www.runoob.com/redis/server-debug-segfault.html">DEBUG SEGFAULT</a>&nbsp;<br />让 Redis 服务崩溃</td>





</tr>
<tr>
<td>21</td>
<td><a href="http://www.runoob.com/redis/server-flushall.html">FLUSHALL</a>&nbsp;<br />删除所有数据库的所有key</td>





</tr>
<tr>
<td>22</td>
<td><a href="http://www.runoob.com/redis/server-flushdb.html">FLUSHDB</a>&nbsp;<br />删除当前数据库的所有key</td>





</tr>
<tr>
<td>23</td>
<td><a href="http://www.runoob.com/redis/server-info.html">INFO [section]</a>&nbsp;<br />获取 Redis 服务器的各种信息和统计数值</td>





</tr>
<tr>
<td>24</td>
<td><a href="http://www.runoob.com/redis/server-lastsave.html">LASTSAVE</a>&nbsp;<br />返回最近一次 Redis 成功将数据保存到磁盘上的时间，以 UNIX 时间戳格式表示</td>





</tr>
<tr>
<td>25</td>
<td><a href="http://www.runoob.com/redis/server-monitor.html">MONITOR</a>&nbsp;<br />实时打印出 Redis 服务器接收到的命令，调试用</td>





</tr>
<tr>
<td>26</td>
<td><a href="http://www.runoob.com/redis/server-role.html">ROLE</a>&nbsp;<br />返回主从实例所属的角色</td>





</tr>
<tr>
<td>27</td>
<td><a href="http://www.runoob.com/redis/server-save.html">SAVE</a>&nbsp;<br />同步保存数据到硬盘</td>





</tr>
<tr>
<td>28</td>
<td><a href="http://www.runoob.com/redis/server-shutdown.html">SHUTDOWN [NOSAVE] [SAVE]</a>&nbsp;<br />异步保存数据到硬盘，并关闭服务器</td>





</tr>
<tr>
<td>29</td>
<td><a href="http://www.runoob.com/redis/server-slaveof.html">SLAVEOF host port</a>&nbsp;<br />将当前服务器转变为指定服务器的从属服务器(slave server)</td>





</tr>
<tr>
<td>30</td>
<td><a href="http://www.runoob.com/redis/server-showlog.html">SLOWLOG subcommand [argument]</a>&nbsp;<br />管理 redis 的慢日志</td>





</tr>
<tr>
<td>31</td>
<td><a href="http://www.runoob.com/redis/server-sync.html">SYNC</a>&nbsp;<br />用于复制功能(replication)的内部命令<br /><br /></td>





</tr>





</tbody>




</table>
<h2>八Java 使用 Redis</h2>
<p><span style="font-size: 18px;"><strong>安装</strong></span></p>
<p>开始在 Java 中使用 Redis 前， 我们需要确保已经安装了 redis 服务及 Java redis 驱动，且你的机器上能正常使用 Java。 Java的安装配置可以参考我们的&nbsp;<a title="Java开发环境配置" href="http://www.runoob.com/redis/java-environment-setup.html" target="_top">Java开发环境配置&nbsp;</a>接下来让我们安装 Java redis 驱动：</p>
<ul>
<li>首先你需要下载驱动包&nbsp;<a href="https://mvnrepository.com/artifact/redis.clients/jedis" rel="nofollow" target="_blank"><strong>下载 jedis.jar</strong></a>，确保下载最新驱动包。</li>
<li>在你的 classpath 中包含该驱动包。</li>




</ul>
<blockquote>
<p>本站提供了 2.9.0 jar 版本下载：&nbsp;<a href="http://static.runoob.com/download/jedis-2.9.0.jar" target="_blank">jedis-2.9.0.jar</a></p>




</blockquote>
<p><span style="font-size: 18px;"><strong>连接到 redis 服务</strong></span></p>
<div class="example">
<p class="example"><strong>实例</strong></p>
<div class="example_code">
<div class="hl-main">
<div class="cnblogs_code">
<pre>import redis.clients.jedis.Jedis; <br /><br /><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> RedisJava { <br /><span style="color: #0000ff;">   public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) { <br /><span style="color: #008000;">   //</span><span style="color: #008000;">连接本地的 Redis 服务 <br />   Jedis jedis = new Jedis("localhost"); <br />   System.out.println("连接成功"); <br /></span><span style="color: #008000;">   //</span><span style="color: #008000;">查看服务是否运行 <br />   System.out.println("服务正在运行: "+jedis.ping()); <br />   } <br />}</span></pre>
</div>
</div>
</div>
</div>
<p>编译以上 Java 程序，确保驱动包的路径是正确的。</p>
<pre class="prettyprint prettyprinted"><span class="pun">连接成功<span class="pln">
<span class="pun">服务正在运行:<span class="pln"> PONG</span></span></span></span></pre>
<p><span style="font-size: 18px;"><strong>Redis Java String(字符串) 实例</strong></span></p>
<div class="example">
<p class="example"><strong>实例</strong></p>
<div class="example_code">
<div class="hl-main">
<div class="cnblogs_code">
<pre><span style="color: #000000;">import redis.clients.jedis.Jedis; 

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> RedisStringJava { 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) { 
     </span><span style="color: #008000;">//</span><span style="color: #008000;">连接本地的 Redis 服务 </span>
     Jedis jedis = <span style="color: #0000ff;">new</span> Jedis(<span style="color: #800000;">"</span><span style="color: #800000;">localhost</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
     System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">连接成功</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
     </span><span style="color: #008000;">//</span><span style="color: #008000;">设置 redis 字符串数据 </span>
     jedis.<span style="color: #0000ff;">set</span>(<span style="color: #800000;">"</span><span style="color: #800000;">runoobkey</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">www.runoob.com</span><span style="color: #800000;">"</span><span style="color: #000000;">);
      </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取存储的数据并输出 </span>
     System.<span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">redis 存储的字符串为: </span><span style="color: #800000;">"</span>+jedis.<span style="color: #0000ff;">get</span>(<span style="color: #800000;">"</span><span style="color: #800000;">runoobkey</span><span style="color: #800000;">"</span>));<br />   } <br />}</pre>
</div>
</div>
</div>
</div>
<p>编译以上程序。</p>
<pre class="prettyprint prettyprinted"><span class="pun">连接成功<span class="pln">
redis <span class="pun">存储的字符串为:<span class="pln"> www<span class="pun">.<span class="pln">runoob<span class="pun">.<span class="pln">com</span></span></span></span></span></span></span></span></pre>
<p><strong><span style="font-size: 18px;">Redis Java List(列表) 实例</span></strong></p>
<div class="example">
<p class="example"><strong>实例</strong></p>
<div class="example_code">
<div class="hl-main">
<div class="cnblogs_code">
<pre><span style="color: #000000;">import java.util.List;
import redis.clients.jedis.Jedis; 

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> RedisListJava {
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) { 
     </span><span style="color: #008000;">//</span><span style="color: #008000;">连接本地的 Redis 服务 </span>
     Jedis jedis = <span style="color: #0000ff;">new</span> Jedis(<span style="color: #800000;">"</span><span style="color: #800000;">localhost</span><span style="color: #800000;">"</span><span style="color: #000000;">);
     System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">连接成功</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
     </span><span style="color: #008000;">//</span><span style="color: #008000;">存储数据到列表中 </span>
     jedis.lpush(<span style="color: #800000;">"</span><span style="color: #800000;">site-list</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Runoob</span><span style="color: #800000;">"</span><span style="color: #000000;">);
     jedis.lpush(</span><span style="color: #800000;">"</span><span style="color: #800000;">site-list</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Google</span><span style="color: #800000;">"</span><span style="color: #000000;">);
     jedis.lpush(</span><span style="color: #800000;">"</span><span style="color: #800000;">site-list</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Taobao</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取存储的数据并输出 </span>
     List&lt;String&gt; list = jedis.lrange(<span style="color: #800000;">"</span><span style="color: #800000;">site-list</span><span style="color: #800000;">"</span>, <span style="color: #800080;">0</span> ,<span style="color: #800080;">2</span><span style="color: #000000;">); 
        </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>; i&lt;list.size(); i++<span style="color: #000000;">) {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">列表项为: </span><span style="color: #800000;">"</span>+list.<span style="color: #0000ff;">get</span><span style="color: #000000;">(i)); 
        } 
    } 
}</span></pre>
</div>
</div>
</div>
</div>
<p>编译以上程序。</p>
<pre class="prettyprint prettyprinted"><span class="pun">连接成功<span class="pln">
<span class="pun">列表项为:<span class="pln"> <span class="typ">Taobao<span class="pln">
<span class="pun">列表项为:<span class="pln"> <span class="typ">Google<span class="pln">
<span class="pun">列表项为:<span class="pln"> <span class="typ">Runoob</span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p><strong><span style="font-size: 18px;">Redis Java Keys 实例</span></strong></p>
<div class="example">
<p class="example"><strong>实例</strong></p>
<div class="example_code">
<div class="hl-main">
<div class="cnblogs_code">
<pre><span style="color: #000000;">import java.util.Iterator; 
import java.util.Set; 
import redis.clients.jedis.Jedis; 

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> RedisKeyJava { 
      </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) { 
      </span><span style="color: #008000;">//</span><span style="color: #008000;">连接本地的 Redis 服务 </span>
      Jedis jedis = <span style="color: #0000ff;">new</span> Jedis(<span style="color: #800000;">"</span><span style="color: #800000;">localhost</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
      System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">连接成功</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
      </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取数据并输出 </span>
      Set&lt;String&gt; keys = jedis.keys(<span style="color: #800000;">"</span><span style="color: #800000;">*</span><span style="color: #800000;">"</span><span style="color: #000000;">); 
      Iterator</span>&lt;String&gt; it=<span style="color: #000000;">keys.iterator() ; 
           </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(it.hasNext()){ 
               String key </span>=<span style="color: #000000;"> it.next(); 
               System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(key); 
           }
      } 
 }</span></pre>
</div>
</div>
</div>
</div>
<p>编译以上程序。</p>
<p class="prettyprint prettyprinted"><span class="pun">连接成功</span></p>
<p class="prettyprint prettyprinted"><span class="pun"><span class="pln"> runoobkey </span></span></p>
<p class="prettyprint prettyprinted"><span class="pun"><span class="pln">site<span class="pun">-<span class="pln">list</span></span></span></span></p>
</div></div>

</body>
</html>
